package com.example.bes952.springboot2.bcs.producer;

import com.example.bes952.springboot2.bcs.modle.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class StreamProducer {

    private static final Logger log = LoggerFactory.getLogger(StreamProducer.class);

    private RedisTemplate redisTemplate;

    public void sendRecord(String streamKey) {
        Book book = new Book();
        book.setTitle("bcs");
        book.setAuthor("bes");
        log.info("产生一本书的信息:[{}]", book);
        ObjectRecord<String, Book> record = StreamRecords.newRecord()
                .in(streamKey)
                .ofObject(book)
                .withId(RecordId.autoGenerate());

        RecordId recordId = redisTemplate.opsForStream()
                .add(record);
        log.info("返回的record-id:[{}]", recordId);
    }
}
